
package managedbean.CRM.SaleInquiry;

import entity.CRM.LineItem;
import entity.CRM.SaleInquiry;
import entity.Employee;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.el.ELContext;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import managedbean.Controller;
import session.stateless.CRM.SaleInquiryBeanLocal;


@ManagedBean(name="viewAllSaleInquiry")
@ViewScoped
public class ViewAllSaleInquiryManagerBean implements Serializable{

    public ViewAllSaleInquiryManagerBean() {}
    
    @EJB
    private SaleInquiryBeanLocal saleInquiryBeanLocal;
    private SaleInquiry selectedSaleInquiry;
    private List<LineItem> lineItems;
    private String statusMessage;

    public SaleInquiry getSelectedSaleInquiry() {
        return selectedSaleInquiry;
    }
    public void setSelectedSaleInquiry(SaleInquiry selectedSaleInquiry) {
        this.selectedSaleInquiry = selectedSaleInquiry;
    }

    public List<LineItem> getLineItems() {
        return lineItems;
    }
    public void setLineItems(List<LineItem> lineItems) {
        this.lineItems = lineItems;
    }
    
    public List<SaleInquiry> getSaleInquiry(){
        return saleInquiryBeanLocal.getAllSaleInquiries();
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    public void editSaleInquiry(ActionEvent event) throws IOException{
    //        SaleInquiry selectedInquiry = (SaleInquiry)event.getComponent().getAttributes().get("selectedSaleInquiry");
        SaleInquiry selectedInquiry =this.getSelectedSaleInquiry();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedSaleInquiry", selectedInquiry);
        FacesContext.getCurrentInstance().getExternalContext().redirect("editSaleInquiry.xhtml");
    }
    
    
    public void deleteSaleInquiry(ActionEvent event) throws IOException{
        try{
            saleInquiryBeanLocal.deleteSaleInquiry(selectedSaleInquiry.getId());
            FacesContext.getCurrentInstance().getExternalContext().redirect("viewSaleInquiry.xhtml");
            statusMessage = "Sale Inquiry Deleted Successfully"; 
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  statusMessage, ""));
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
       
    }
    
    public void createSaleQuotation(ActionEvent event) throws IOException
    {
        SaleInquiry selectedInquiry = this.getSelectedSaleInquiry();
        try
        {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedSaleInquiry", selectedInquiry);
            FacesContext.getCurrentInstance().getExternalContext().redirect("../salequotation/addQuotationFromInquiry.xhtml");
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
}